package com.weetop.akly.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.weetop.akly.R;
import com.weetop.akly.bean.MemberBean;
import com.weetop.akly.bean.PostMessage;
import com.weetop.akly.utils.EventBusUtil;

import java.util.ArrayList;

public class MyGridSpryAdapter extends
        RecyclerView.Adapter<MyGridSpryAdapter.ViewHolder> {
    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    private LayoutInflater mInflater;
    private ArrayList<MemberBean> list = new ArrayList<>();
    private int selectMax = 9;
    private Context context;
    private boolean isShowDelete = false;
    /**
     * 点击添加审批人员跳转
     */
    private onAddSpryClickListener mOnAddSpryClickListener;

    public interface onAddSpryClickListener {
        void onAddSpryClick();
    }

    public MyGridSpryAdapter(Context context, onAddSpryClickListener mOnAddSpryClickListener) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mOnAddSpryClickListener = mOnAddSpryClickListener;
    }

    public MyGridSpryAdapter(Context context, onAddSpryClickListener mOnAddPicClickListener, boolean isShowDelete) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mOnAddSpryClickListener = mOnAddPicClickListener;
        this.isShowDelete = isShowDelete;
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setList(ArrayList<MemberBean> list) {
        this.list = list;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mImg;
        ImageView iv_right_arrow;
        LinearLayout ll_del;
        TextView tv_spr_name;

        public ViewHolder(View view) {
            super(view);
            mImg = (ImageView) view.findViewById(R.id.fiv);
            iv_right_arrow = (ImageView) view.findViewById(R.id.iv_right_arrow);
            ll_del = (LinearLayout) view.findViewById(R.id.ll_del);
            tv_spr_name = (TextView) view.findViewById(R.id.tv_spr_name);
        }
    }

    @Override
    public int getItemCount() {
        if (list.size() < selectMax) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_spry,
                viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
//        viewHolder.setIsRecyclable(false);
        return viewHolder;
    }

    private boolean isShowAddItem(int position) {
        int size = list.size() == 0 ? 0 : list.size();
        return position == size;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.mImg.setImageResource(R.drawable.addimg_1x);
            viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddSpryClickListener.onAddSpryClick();
                }
            });
            viewHolder.ll_del.setVisibility(View.INVISIBLE);
            viewHolder.tv_spr_name.setVisibility(View.INVISIBLE);
            viewHolder.iv_right_arrow.setVisibility(View.INVISIBLE);
        } else {
            MemberBean memberBean = list.get(position);
            if (isShowDelete) {
                viewHolder.ll_del.setVisibility(View.VISIBLE);
            } else {
                viewHolder.ll_del.setVisibility(View.GONE);
            }
            viewHolder.iv_right_arrow.setVisibility(View.VISIBLE);
            viewHolder.tv_spr_name.setVisibility(View.VISIBLE);
            viewHolder.tv_spr_name.setText(memberBean.getName());
            viewHolder.ll_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int index = viewHolder.getAdapterPosition();
                    // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                    // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                    if (index != RecyclerView.NO_POSITION) {
                        list.remove(index);
                        notifyItemRemoved(index);
                        notifyItemRangeChanged(index, list.size());
                        EventBusUtil.sendEvent(new PostMessage("updataimgcount"));
                    }
                }
            });
//            GlideUtil.load(context, viewHolder.mImg, memberBean.getPhoto(), R.mipmap.headimg, R.mipmap.headimg);
            Glide.with(context)
                    .load(memberBean.getPhoto())
                    .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                    .apply(new RequestOptions()
                            .transforms(new CenterCrop(), new RoundedCorners(100))
                            .placeholder(R.mipmap.headimg)//图片加载出来前，显示的图片
                            .fallback(R.mipmap.headimg)
                            .error(R.mipmap.headimg))
                    .into(viewHolder.mImg);
            //itemView 的点击事件
            if (mItemClickListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int adapterPosition = viewHolder.getAdapterPosition();
                        mItemClickListener.onItemClick(adapterPosition, v);
                    }
                });
            }
        }
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}
